<?php
/**
 * Created by PhpStorm.
 * User: poptser@foxmail.com
 * Date: 2018/1/25
 * Time: 15:52
 */

namespace App\Services\SortingWeb;
use Illuminate\Support\Facades\DB;

class ToolService{

    //获取拣货单号
    public function getSortingSn($sortingId){
        return DB::table('sorting_order')->where("id",$sortingId)->value('sorting_sn');
    }

    //配送区
    public function getDeliver($wdr_id){
        return DB::table("warehouse_deliver_region")->where("id",$wdr_id)->select(['*'])->first();
    }

    //best time 转字符串
    public function bestToStr($bestTimeStart,$bestTime){
        return date('Y-m-d H:i',$bestTimeStart)."-".date('H:i',$bestTime);
    }

    //获取订单号
    public function getOrderSn($orderId){
        return DB::table('order_info')->where("order_id",$orderId)->value('order_sn');
    }

    //获取划区名称
    public function getAreaName($AreaId){
        return DB::table('zoning_cate')->where("id",$AreaId)->value('name');
    }

    //划区
    public function getAllDeliveryArea(){
        return DB::table('zoning_cate')->select(['id','name'])->get();
    }
    //配送时间
    public function getDeliverTime(){
        return DB::table('distribution_time')->select(['id','start_time','end_time'])->get();
    }
    //配送商
    public function getWarehouse(){
        return DB::table('distribution_company')->select(['id','lite_name'])->get();
    }
    //获取退货工单类型
    public function getRefundWorkType(){
        return DB::table('order_service_work_type')->select(['type_id','type_name'])->get();
    }

    public function getRefundWorkTypeById($refundType){
        return DB::table('order_service_work_type')->where("type_id",$refundType)->value('type_name');
    }

    //获取每个分拣单最大订单数
    public function getSortingMaxNum($wid){
        return DB::table('sorting_config')->where("wid",$wid)->value('sorting_max_num');
    }

    //设置每个拣货单最大订单数
    public function setSortingMaxNum($maxNum = 5,$wid){
        return DB::table('sorting_config')
            ->where('wid', $wid)
            ->update(['sorting_max_num' => $maxNum]);
    }
    //获取用户账号
    public function getNameById(int $user_id){
        return DB::table('admin_user')->where("user_id",$user_id)->value('user_name');
    }
    //获取用户仓库ID
    public function getUserWid(int $user_id){
        return DB::table('warehouse_personnel')->where("uid",$user_id)->value('wid');
    }
    //获取仓库详情
    public function getWidInfo(int $wid){
        return DB::table('warehouse')->where("id",$wid)->first(['*']);
    }

    /**
     * 拣货单状态转换
     * Author:poptser@foxmail.com
     * @param $status
     * @return string
     */
    public function sortingToString($status){
        switch ($status){
            case "1":
                return "未配货";
                break;
            case "2":
                return "已获取";
                break;
            case "3":
                return "配货中";
                break;
            case "4":
                return "配货完成";
                break;
            case "5":
                return "已取消";
                break;
            default:
                return "未知";
        }
    }

    /**
     * 订单状态ID换字符串
     * Author:poptser@foxmail.com
     * @param $order_status
     * @return string
     */
    public function orderToString($order_status){
        switch ($order_status){
            case "1":
                return "已确认";
                break;
            case "2":
                return "<span style='color: red'>已取消</span>";
                break;
            case "5":
                return "已分单";
                break;
        }
    }

    /**
     * 发货状态ID换字符串
     * Author:poptser@foxmail.com
     * @param $shipping_status
     * @return string
     */
    public function shippingToString($shipping_status){
        switch ($shipping_status){
            case "0":
                return "未发货";
                break;
            case "1":
                return "配送中";
                break;
            case "2":
                return "已收货";
                break;
            case "3":
                return "拣货中";
                break;
            case "5":
                return "已拣货";
                break;
        }
    }

    /**
     * 支付状态ID换字符串
     * Author:poptser@foxmail.com
     * @param $pay_status
     * @return string
     */
    public function payToString($pay_status){
        switch ($pay_status){
            case "0":
                return "未付款";
                break;
            case "2":
                return "已付款";
                break;
            case "3":
                return "部分退款";
                break;
            case "4":
                return "<span style='color: red'>全部退款</span>";
                break;
        }
    }

    /**
     * 订单物流记录
     * Author:poptser@foxmail.com
     * @param $order_id
     * @param $shipping_status
     * @param $remark
     */
    public function recordLogistics($order_id, $shipping_status, $remark){
        $sorting_id = DB::table('logistics')->insertGetId(
            [
                'remark' => $remark,
                'shipping_status' => $shipping_status,
                'log_time' => gm_time(),
                'order_id' => $order_id
            ]
        );
    }
}